﻿using Autofac.Core;
using IntelligentAgriculture.Contracts.Dto.Input;
using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Services.StartServices;
using IntelligentAgriculture.WebAPI.Application.Command.GermlineHistoryRecordsCommand;
using IntelligentAgriculture.WebAPI.Application.Command.StartCommand;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace IntelligentAgriculture.WebAPI.Controllers
{
    /// <summary>
    /// 种母历史记录信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HistoryController : ControllerBase
    {
        /// <summary>
        /// 中介者模式
        /// </summary>
        private readonly IMediator _mediator;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<HistoryController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者模式</param>
        /// <param name="logger">日志</param>
        public HistoryController(IMediator mediator, ILogger<HistoryController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }



        /// <summary>
        /// 种母历史记录信息显示查询
        /// </summary>
        /// <param name="input">输入参数</param>
        /// <returns>返回数据</returns>
        [HttpGet]
        public async Task<Results<PageOutput<HistoryOutPut>>> GetHistoryList([FromQuery]HistoryInput? input)
        {
            try
            {
                _logger.LogInformation("这是显示查询种母历史记录表");
                var list = new SelectHistoryRecordsCommand(input);
                return await _mediator.Send(list);
            }
            catch (Exception ex)
            {
                _logger.LogError("这是显示查询种母历史记录表发生异常" + ex.Message);
                throw;
            }
        }



        /// <summary>
        /// 逻辑删除种母历史记录信息
        /// </summary>
        /// <param name="command">主键id</param>
        /// <returns>受影响的行数</returns>
        /// 

        [HttpDelete]
        public async Task<Results<int>> DeleteHistoryrecord(DeleteHistoryRecordCommand command)
        {
            try
            {
                _logger.LogInformation("这是种母历史记录信息逻辑删除");


                return await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                _logger.LogError("这是种母历史记录信息逻辑删除发生异常" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 逻辑批量删除种母历史记录信息
        /// </summary>
        /// <param name="command">主键id</param>
        /// <returns>受影响的行数</returns>
        /// 

        [HttpPut]
        public async Task<Results<int>> DeleteRangeHistoryrecord(DeleteRangeHistoryRecordCommand command)
        {
            try
            {
                _logger.LogInformation("这是种母历史记录信息逻辑批量删除");


                return await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                _logger.LogError("这是种母历史记录信息逻辑批量删除发生异常" + ex.Message);
                throw;
            }
        }


        /// <summary>
        /// 添加种母历史记录信息
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns>受影响的行数</returns>
        /// 

        [HttpPost]
        public async Task<Results<int>> AddHistoryrecordAsync(List<Historyrecord> input)
        {
            try
            {
                _logger.LogInformation("这是种母历史记录信息添加");


                var command = new AddHistoryCommand(input);
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError("这是种母历史记录信息添加发生异常" + ex.Message);
                throw;
            }
        }



        /// <summary>
        /// 反填种母历史记录表
        /// </summary>
        /// <param name="earTag">耳号</param>
        /// <returns>返回数据</returns>
        [HttpGet]
        public async Task<Results<HistoryOutPut>> GetHistoryByIdAsync(string? earTag)
        {
            try
            {
                _logger.LogInformation("这是反填种母历史记录表");
                var query = new BackfillHistoryCommand(earTag);
                var history = await _mediator.Send(query);
                return history;
            }
            catch (Exception ex)
            {
                _logger.LogError("这是反填种母历史记录表发生异常" + ex.Message);
                throw;
            }

        }


        /// <summary>
        /// 修改种母历史记录表
        /// </summary>
        /// <param name="command">实体</param>
        /// <returns>受影响的行数</returns>
        [HttpPut]
        public async Task<Results<int>> UpdateHisRecordAsync(UpdateHistoryCommand command)
        {
            try
            {
                _logger.LogInformation("这是修改种母历史记录表");
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError("这是修改种母历史记录表发生异常" + ex.Message);
                throw;
            }

        }






    }
}
